package com.rcd.viatornisl.android.model.adapters;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rcd.viatornisl.android.R;
import com.rcd.viatornisl.android.model.Establishment;

public class EstablishmentAdapter extends ArrayAdapter<Establishment> {
		
	public static class EstablishmentHolder {
		public ImageView _EstablishmentLogo;
		public TextView _EstablishmentName;
		public TextView _EstablishmentSlogan;
		public TextView _EstablishmentCountry;
		public Bundle _EstablishmentBundle;
	}

	public EstablishmentAdapter(Context context, int resource, List<Establishment> establishments) {
		// Default operations
		super(context, resource, establishments);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// Local variables
		EstablishmentHolder establishmentHolder = null;
		Establishment establishment = this.getItem(position);
		LayoutInflater inflater = LayoutInflater.from(this.getContext());

		// If is a creation
		if (convertView == null) {
			// Inflate the layout
			convertView = inflater.inflate(R.layout.establishment_list_item, null);
						
			// Create the holder
			establishmentHolder = new EstablishmentHolder();
			
			// Setup the holder
			establishmentHolder._EstablishmentLogo = ((ImageView)convertView.findViewById(R.id.establishmentLogo));
			establishmentHolder._EstablishmentName = ((TextView)convertView.findViewById(R.id.establishmentName));
			establishmentHolder._EstablishmentSlogan = ((TextView)convertView.findViewById(R.id.establishmentSlogan));
			establishmentHolder._EstablishmentCountry = ((TextView)convertView.findViewById(R.id.establishmentCountry));
			establishmentHolder._EstablishmentBundle = new Bundle();
			
			// Set the parameters to the view
			convertView.setTag(establishmentHolder);
		} 
			
		// Cast the holder
		establishmentHolder = ((EstablishmentHolder)convertView.getTag());

		// Create a bitmap object to hold the establishment logo
		Bitmap establishmentLogo = BitmapFactory.decodeByteArray(establishment.getLogo(), 0, establishment.getLogo().length);
		
		// Check if the bitmap object is valid
		if (establishmentLogo == null) {
			// Set the default establishment logo
			establishmentHolder._EstablishmentLogo.setImageResource(R.drawable.ic_default_establishment_logo);
		} else {
			// Set the bitmap object as establishment logo
			establishmentHolder._EstablishmentLogo.setImageBitmap(establishmentLogo);	
		}
		
		// Setup the holder
		establishmentHolder._EstablishmentName.setText(establishment.getName());
		establishmentHolder._EstablishmentSlogan.setText(establishment.getSlogan());
		establishmentHolder._EstablishmentCountry.setText(establishment.getCountry());
		establishmentHolder._EstablishmentBundle.putInt("establishmentId", establishment.getId());

		// Provide the return
		return convertView;
	}

}
